package com.zzyl.nursing.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.dto.NursingProjectPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author zhuyan
 * @date 2025-10-08
 */
@Service
public class NursingPlanServiceImpl implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;


    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlan selectNursingPlanById(Long id)
    {
        return nursingPlanMapper.selectNursingPlanById(id);
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto dto)
    {

        // 1、保存护理计划

        NursingPlan nursingPlan = new NursingPlan();

        // nursingPlan.setSortNo(dto.getSortNo());
        // nursingPlan.setPlanName(dto.getPlanName());
        // nursingPlan.setStatus(dto.getStatus());
        BeanUtils.copyProperties(dto, nursingPlan); // 属性名 数据类型要一致
        nursingPlan.setCreateTime(new Date());
        nursingPlanMapper.insertNursingPlan(nursingPlan);

        // 2、护理项目和护理计划中间表

        List<NursingProjectPlanDto> projectPlans = dto.getProjectPlans();

        // 把dto转成实体类
        List<NursingProjectPlan> projectPlanList = projectPlans.stream().map(d -> {
            NursingProjectPlan projectPlan = new NursingProjectPlan();
            projectPlan.setPlanId(nursingPlan.getId());   //护理计划的id
            projectPlan.setProjectId(Integer.parseInt(d.getProjectId()));
            projectPlan.setExecuteTime(d.getExecuteTime());
            projectPlan.setCreateTime(new Date());
            projectPlan.setExecuteCycle(d.getExecuteCycle());
            projectPlan.setExecuteFrequency(d.getExecuteFrequency());
            return projectPlan;
        }).collect(Collectors.toList());

        nursingProjectPlanMapper.insertBacth(projectPlanList);
        return 1;
    }


    /**
     * 修改护理计划
     *
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlan nursingPlan)
    {
        nursingPlan.setUpdateTime(DateUtils.getNowDate());
        return nursingPlanMapper.updateNursingPlan(nursingPlan);
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return nursingPlanMapper.deleteNursingPlanByIds(ids);
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        return nursingPlanMapper.deleteNursingPlanById(id);
    }
}
